package com.lh.springcloud.controller;

import com.lh.springcloud.bean.CommonResult;
import com.lh.springcloud.bean.Payment;
import com.lh.springcloud.lb.LoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


import java.net.URI;
import java.util.List;

@RestController
@Slf4j
public class OrderController {

    //public static final String PAYMENT_URL = "http://localhost:8001";
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    private RestTemplate restTemplate;

    //可以获取注册中心上的服务列表
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancer loadBalancer;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment) {
        //该方法用于创建支付记录，接收一个 Payment 对象作为参数，并通过 restTemplate 发送 POST 请求到 PAYMENT_URL + "payment/create" 地址，返回值为 CommonResult 对象。
        //postForObject 方法用于发送一个 POST 请求，并将请求结果转换为指定的对象。
        return restTemplate.postForObject(PAYMENT_URL + "payment/create", payment, CommonResult.class);
    }

    //返回对象为响应体中数据转化成的对象，基本上可以理解为Json
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }

    //返回对象为ResponseEntity对象，包含了响应中的一些重要信息，比如响应头、响应状态码、响应体等
    @GetMapping("/consumer/payment/getForEntity/{id}")
    public CommonResult<Payment> getPayment2(@PathVariable("id") Long id) {
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful()) {
            log.info("支付服务(get)获取支付结果：{}", entity.getBody() + "\t" + entity.getBody());
            return entity.getBody();
        } else {
            return new CommonResult<>(444, "操作失败");
        }
    }


    @GetMapping("/consumer/payment/lb")
    public String getPaymentLB() {
        // 从服务注册中心获取 "CLOUD-PAYMENT-SERVICE" 的服务实例列表
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");

        // 检查服务实例是否可用
        if (instances == null || instances.size() <= 0) {
            // 如果没有可用的实例，返回 null
            return null;
        }

        // 使用负载均衡器选择一个服务实例
        ServiceInstance serviceInstance = loadBalancer.instances(instances);

        // 提取所选服务实例的 URI
        URI uri = serviceInstance.getUri();
        System.out.println(uri);

        // 使用 RestTemplate 发送 GET 请求到所选服务实例的 "/payment/lb" 端点
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }

    // ====================> zipkin+sleuth
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin(){
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }

}
